
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class SplashScreen extends Canvas {
	
	final int DISMISS_TIME = 3000;
	private Display display;
	private Displayable next;
	private Timer timer;
	private Image image;
	
	public SplashScreen(Display d, Displayable next) {
		this.display = d;
		this.next = next;
		
		timer = new Timer();
		
		try {
			image = Image.createImage("/logo.gif");
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		display.setCurrent(this);
	}
	
	protected void dismiss() {
		timer.cancel();
		display.setCurrent(next);
	}
	
	protected void keyPressed(int keyCode) {
		dismiss();
	}
	
	protected void pointerPressed(int x, int y) {
		dismiss();
	}

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
		g.setFont(font);
		g.setColor(0x00FFFFFF);
		g.fillRect(0, 0, getWidth(), getHeight());
		
		g.setColor(0x00000000);
		g.drawString("N Puzzle", getWidth() / 2, getHeight() / 2 - 70,
				Graphics.HCENTER | Graphics.BOTTOM);
		g.drawImage(image, getWidth() / 2, getHeight() / 2,
				Graphics.HCENTER | Graphics.VCENTER);
		g.drawString("(C) Copyright HEDSPI IS1", getWidth() / 2, getHeight(),
				Graphics.HCENTER | Graphics.BOTTOM);
	}
	
	protected void showNotify() {
		timer.schedule(new CountDown(this), DISMISS_TIME);
	}
}

class CountDown extends TimerTask {
	
	private final SplashScreen splashScreen;
	
	public CountDown(SplashScreen splashScreen) {
		this.splashScreen = splashScreen;
	}
	
	public void run() {
		// TODO Auto-generated method stub
		splashScreen.dismiss();
	}
	
}